<template>
	<!-- 消防监控室制表记录表 -->
	<view class="">
		<view class="files">
			<view class="tabs">
				<fui-list   bottomBorder title="巡查人员基本信息">
					<view class="tab">
						<fui-list-cell  >
							<text >巡查人员</text>
							<text class="fui-text__explain">{{inspectionStaff.name}}</text>
						</fui-list-cell>
						
						<fui-list-cell  >
							<text >巡查日期</text>
							<text class="fui-text__explain">{{inspectionStaff.data}}</text>
						</fui-list-cell>
						
						<fui-list-cell  >
							<text >巡查时段</text>
							<text class="fui-text__explain">{{inspectionStaff.time}}</text>
						</fui-list-cell>
						
					</view>
				</fui-list>
				
				<fui-list   bottomBorder title="火灾报警控制器运行情况">
					<view class="tab">
						<fui-input required @click="showOpsOne =!showOpsOne" v-model="fireAlarm.dutySituation" label="值班情况" readonly textAlign="right"  placeholder="请选择" ></fui-input>
						<fui-input  required  label="火警" v-model="fireAlarm.fireAlarm"   textAlign="right" ></fui-input>
						<fui-input  required  label="火警错报" v-model="fireAlarm.misstatement"   textAlign="right" ></fui-input>
						<fui-input  required  label="故障报警" v-model="fireAlarm.faultAlarm"   textAlign="right" ></fui-input>
						<fui-input  required   label="监管报警" v-model="fireAlarm.supervisionAlarm"   textAlign="right" ></fui-input>
						<fui-input label="报警、故障部位、原因及处理情况" readonly></fui-input>
						<u--textarea  placeholder="请输入内容" v-model="fireAlarm.description" count maxlength="250"   ></u--textarea>
					</view>
				</fui-list>
				
				<fui-list   bottomBorder title="控制室内其他消防系统运行情况">
					<view class="tab">
						<fui-input required label="控制状态" v-model="controlRoom.controlState" @click="showOpsTow =!showOpsTow" readonly textAlign="right"  placeholder="请选择" ></fui-input>
						<fui-input required label="运行状态" @click="showOpsThree =!showOpsThree" v-model="controlRoom.runningState" readonly textAlign="right"  placeholder="请选择" ></fui-input>
						<fui-input label="报警、故障部位、原因及处理情况" readonly></fui-input>
						<u--textarea  placeholder="请输入内容" v-model="controlRoom.callThePolice" count maxlength="250"   ></u--textarea>
					</view>
				</fui-list>
				
			</view>
			
			<view class="tab_ops">
			<!-- 	<view style="width: 100%; display: flex; justify-content: space-between;">
					<fui-button btnSize="small" >巡查人员签字</fui-button>
					<fui-button btnSize="small"  >安全主管签字</fui-button>
				</view> -->
				<view style="padding-top: 40rpx;">
					<fui-button @click="submit" type="success">提交</fui-button>
				</view>
			</view>
			<view >
				<u-picker  @confirm="onOpsOne" @cancel="showOpsOne = !showOpsOne" :show="showOpsOne" :columns="columnsOne"></u-picker>
			</view>
			<view >
				<u-picker  @confirm="onOpsTow" @cancel="showOpsTow = !showOpsTow" :show="showOpsTow" :columns="columnsTow"></u-picker>
			</view>
			<view >
				<u-picker  @confirm="onOpsThree" @cancel="showOpsThree = !showOpsThree" :show="showOpsThree" :columns="columnsThree"></u-picker>
			</view>
		</view>
	</view>
</template>

<script>
	import moment, { duration } from "moment"
	import {
		BASE_IP
	} from '@/common/http/url.js'
// import test from "../../../uni_modules/uview-plus/libs/function/test"
// import text from "../../../uni_modules/uview-plus/libs/config/props/text"
	import tools from "@/tools/tools.js"
	export default{
		name : "fileMonitoringControl",
		data(){
			return{
				text : "次",
				user : "",
				inspectionStaff : {
					name : "",
					data : "",
					time : ""
				}, 
				columnsOne : [['正常','异常']],
				columnsTow :[['正常','异常']],
				columnsThree : [['正常','异常']],
				showOpsOne : false,
				showOpsTow : false,
				showOpsThree : false,
				fireAlarm : {
					dutySituation : "正常",
					fireAlarm : 0 , //火警
					misstatement : 0,//火警报警
					faultAlarm : 0,//故障报警
					supervisionAlarm : 0, //监管报警
					description : ""
				},
				controlRoom : {
					controlState : "正常", //运行状态
					runningState :"正常", //控制状态
					callThePolice : ""//报警、故障部位、原因及处理情况
				},
				regime : "",
				isSubmit : false,
				preset : ""
			}
		},
		methods : {
			/**
			 * @param {Object} path 转入要签字的路径
			 */
			onSignatures(path) {
				let user = uni.getStorageSync("user")
				if (user.signature == "" || user.signature == undefined || user.signature == null) {
					uni.showModal({
						title: "提示",
						content: "用户未上传签名请前往“预设功能-添加操作员-输入账号-添加签名”，如果已添加操作员上传签名即可",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								return
							}
						}
					})
					return
				}
				let docPath = path
				let url = BASE_IP + user.signature
				tools.instImgPdf(path,user.signature,620,400)
			},
			/** 签字函数
			 * @param {Object} filePath 签字图片
			 * @param {Object} docPath  要签字的文档
			 */
			upSignatures(filePath, docPath) {
				console.log(filePath, docPath)
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/regulatory/signature/word"
				uni.uploadFile({
					header: {
						"Authorization": user.token,
					},
					name: "file",
					url: url,
					formData: {
						"unitId": user.unitId,
						"path": docPath
					},
					filePath: filePath,
					success: (res) => {
						if (res.statusCode == 500) {
							uni.showToast({
								title: "上传失败",
								icon: "error"
							})
							return
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "上传失败,请重试",
							icon: "error"
						})
					}
				})
			},
			/**
			 * 盖章
			 * @param {Object} path 转入要盖章的文件路径
			 */
			onSeal(path) {
				let pralldata = uni.getStorageSync("pralldata")
				// console.log(pralldata)
				let docPath = path
				if (pralldata.unitFireSeal == "" || pralldata.unitFireSeal == undefined) {
					uni.showModal({
						title: "提示",
						content: "单位未上传消防专用章",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								return
							}
						}
					})
					return
				}
				let url = BASE_IP + pralldata.unitFireSeal
				tools.instImgPdf(path,pralldata.unitFireSeal,100,400)
				// console.log(url)
				// uni.downloadFile({ //下载
				// 	url: url, //图片下载地址
				// 	success: res => {
				// 		if (res.statusCode === 200) {
				// 			uni.saveFile({
				// 				tempFilePath: res.tempFilePath,
				// 				success: (r) => {
				// 					// console.log(r)
				// 					this.upLoadSeal(r.savedFilePath, docPath)
				// 				}
				// 			})
				// 		}
				// 	}
				// });
			},
			/** 盖章函数
			 * @param {Object} filePath 印章图片
			 * @param {Object} docPath  要盖章的文档
			 */
			upLoadSeal(filePath, docPath) {
				let user = uni.getStorageSync("user")
				let url = BASE_IP + "pc/regulatory/update/word"
				uni.uploadFile({
					header: {
						"Authorization": user.token,
					},
					name: "file",
					url: url,
					formData: {
						"unitId": user.unitId,
						"path": docPath
					},
					filePath: filePath,
					success: (res) => {
						// console.log(res)
						if (res.statusCode == 500) {
							uni.showToast({
								title: "盖章失败",
								icon: "error"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								title: "盖章失败",
								icon: "error"
							})
							return
						}
					},
					fail: (res) => {
						uni.showToast({
							title: "盖章失败,请重试",
							icon: "error"
						})
					}
				})
			},
			onOpsOne(res){
				this.fireAlarm.dutySituation = res.value[0]
				console.log(res)
				this.showOpsOne = !this.showOpsOne
			},
			onOpsTow(res){
				this.controlRoom.controlState = res.value[0]
				console.log(res)
				this.showOpsTow = !this.showOpsTow
			},
			onOpsThree(res){
				this.controlRoom.runningState = res.value[0]
				console.log(res)
				this.showOpsThree = !this.showOpsThree
			},
			submit(){
				if(this.isSubmit){
					uni.showToast({
						title:"请勿重复提交",
						icon:"error"
					})
					return
				}
				
				
				if(this.fireAlarm.dutySituation == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon:"error"
					})
					return
				}
				
				if(this.controlRoom.controlState == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon:"error"
					})
					return
				}
				
				if(this.controlRoom.runningState == ""){
					uni.showToast({
						title:"表单填写不完整",
						icon:"error"
					})
					return
				}
				// fireAlarm : {
				// 	dutySituation : "正常",
				// 	fireAlarm : 0 , //火警
				// 	misstatement : 0,//火警报警
				// 	faultAlarm : 0,//故障报警
				// 	supervisionAlarm : 0, //监管报警
				if(!this.fireAlarm.fireAlarm){
					this.fireAlarm.fireAlarm =0
				}
				if(!this.fireAlarm.misstatement){
					this.fireAlarm.misstatement =0
				}
				if(!this.fireAlarm.faultAlarm){
					this.fireAlarm.faultAlarm =0
				}
				if(!this.fireAlarm.supervisionAlarm){
					this.fireAlarm.supervisionAlarm =0
				}
				
				let json1 = {
					name  : this.inspectionStaff.name,
					data :this.inspectionStaff.data,
					time : this.inspectionStaff.time,
					
					dutySituation : this.fireAlarm.dutySituation,
				     fireAlarm : this.fireAlarm.fireAlarm,
					 misstatement : this.fireAlarm.misstatement,
					 faultAlarm : this.fireAlarm.faultAlarm,
					 supervisionAlarm : this.fireAlarm.supervisionAlarm,
					 
					 description : this.fireAlarm.description,
					 
					 controlState : this.controlRoom.controlState,
					 runningState : this.controlRoom.runningState,
					 callThePolice : this.controlRoom.callThePolice,
				}
				console.log(json1)
				if(json1.dutySituation == "异常"){
					if(json1.description == "" || json1.description == "无"){
						uni.showModal({
							title:"提示",
							content:"值班情况异常\n请填写相关处理情况",
							showCancel:false
						})
						return
					}
				}
				if(json1.controlState == "异常"){
					if(json1.callThePolice == "" || json1.callThePolice == "无"){
						uni.showModal({
							title:"提示",
							content:"控制状态异常\n请填写相关处理情况",
							showCancel:false
						})
						return
					}
				}
				if(json1.runningState == "异常"){
					if(json1.callThePolice == "" || json1.callThePolice == "无"){
						uni.showModal({
							title:"提示",
							content:"运行状态异常\n请填写相关处理情况",
							showCancel:false
						})
						return
					}
				}
				
				let flag_storage = true
				let user = uni.getStorageSync("user")
				if (user.signature == "" || user.signature == undefined || user.signature == null) {
					uni.showModal({
						title: "提示",
						content: "用户未上传签名请前往“预设功能-添加操作员-输入账号-添加签名”，如果已添加操作员上传签名即可",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								flag_storage = false
								uni.navigateTo({
									url: "/pages/preinstall/preinstall"
								})
								return
							}
						}
					})
					return
				}
				
				if (!flag_storage) {
					return
				}
				
				let pralldata = this.preset
				console.log(pralldata)
				let falg_seal = true
				if (pralldata.unitFireSeal == "" || pralldata.unitFireSeal == undefined) {
					uni.showModal({
						title: "提示",
						content: "单位未上传消防专用章,请前往“预设功能-单位消防专用章”上传",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								falg_seal = false
								uni.navigateTo({
									url: "/pages/preinstall/preinstall"
								})
								return
							}
						}
					})
					return
				}
				
				if (!falg_seal) {
					return
				}
				
				uni.showLoading({
					title:"正在提交"
				})
				let url = BASE_IP + "pc/record/record/fire/monitoring/control"
				let classifyId = this.regime.classifyId
				// console.log(this.regime)
				
				this.isSubmit =!this.isSubmit
				uni.request({
					url:url,
					header: {
						"Authorization": user.token,
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data:{
						unitId: user.unitId,
						name: user.nickName,
						classifyId: classifyId,
						json : JSON.stringify(json1),
						uuid : user.uuid
					},
					method:"POST",
					success: (res) => {
						console.log(res)
						
						if(res.statusCode == 200){
							uni.showToast({
								title:"上传成功",
								icon:"success"
							})
							
							// this.onSeal(res.data.path)
							// this.onSignatures(res.data.path)
							setTimeout((r)=>{
								uni.navigateBack({
								    delta: 1,
								    success: function () {
								        const pages = getCurrentPages(); //获取当前页面栈
								        const prevPage = pages[pages.length - 1]; //获取上一个页面实例对象
								        prevPage.getCollapseDatas(); //调用上一个页面的onLoad方法
								    }
								});
							},1500)
							return
						}
						
						if(res.statusCode == 500){
							uni.showToast({
								title:"上传失败",
								icon:"error"
							})
							this.isSubmit =!this.isSubmit
							return
						}
						if(res.statusCode == 404){
							uni.showToast({
								title:"上传失败",
								icon:"error"
							})
							this.isSubmit =!this.isSubmit
							return
						}
					},fail: (res) => {
						uni.showToast({
							title:"上传失败",
							icon:"error"
						})
						this.isSubmit =!this.isSubmit
					}
				})
			},
			init(){
				uni.showModal({
					title:"提示",
					content:"是否全部正常",
					cancelText:"否",
					confirmText:"是",
					success: (res) => {
						if(res.cancel){
							// fireAlarm : {
							// 	dutySituation : "正常",
							// 	fireAlarm : 1 , //火警
							// 	misstatement : 2,//火警报警
							// 	faultAlarm : 3,//故障报警
							// 	supervisionAlarm : 4, //监管报警
							// 	description : ""
							// },
							// this.fireAlarm.dutySituation = ""
							// // controlRoom : {
							// // 	controlState : "正常", //运行状态
							// // 	runningState :"正常", //控制状态
							// // 	callThePolice : ""//报警、故障部位、原因及处理情况
							// // },
							// this.controlRoom.controlState = ""
							// this.controlRoom.runningState = ""
						}
						if(res.confirm){
							uni.showLoading({
								title:"正在提交"
							})
							this.submit()
						}
					}
				})
			},
			getPrall() { //获取预设功能对象数据
			let pralldata = uni.getStorageSync("pralldata")
			this.preset = pralldata
				// let user = uni.getStorageSync("user")
				// let url = BASE_IP + "pc/presetfunction/find/presetFunction/" + user.unitId + "/" + user.uuid
				// uni.request({
				// 	header: {
				// 		"Authorization": user.token,
				// 	},
				// 	url: url,
				// 	success: (res) => {
				// 		this.preset = res.data
				// 	},
				// 	fail: (res) => {
				// 		// console.log("请求失败：", res)
				// 	}
				// })
			}
		},
		onLoad(ops) {
			uni.setNavigationBarTitle({
				title:"消防监控室制表记录表"
			})
			
			let user = uni.getStorageSync("user")
			console.log(user)
			this.regime = JSON.parse(ops.regime)
			console.log(this.regime)
			this.inspectionStaff.name = user.nickName
			this.getPrall()
			const currentDate = moment();
			const year = currentDate.year();
			const month = currentDate.month() + 1;
			const day = currentDate.date();
			
			this.inspectionStaff.data = year + "-" +month + "-" + day
			
			let pralldata = uni.getStorageSync("pralldata")
			
			let chartson = uni.getStorageSync("chartson")
			console.log(chartson)
			// this.fireAlarm.fireAlarm = chartson.temp2.data[0]
			// if(!this.fireAlarm.fireAlarm){
			// 	this.fireAlarm.fireAlarm  = 0
			// }
			// this.fireAlarm.misstatement = chartson.temp3.data[0]
			// console.log("火警错报：",chartson.temp3.data[0])
			// console.log("火警错报：",this.fireAlarm.misstatement)
			// if(!this.fireAlarm.misstatement || this.fireAlarm.misstatement === undefined){
			// 	this.fireAlarm.misstatemen = 0
			// }
			// this.fireAlarm.faultAlarm = chartson.temp4.data[0]
			// if(!this.fireAlarm.faultAlarm){
			// 	this.fireAlarm.faultAlarm = 0
			// }
			// this.fireAlarm.supervisionAlarm = chartson.temp5.data[0]
			// if(!this.fireAlarm.supervisionAlarm){
			// 	this.fireAlarm.supervisionAlarm =0
			// }
			this.inspectionStaff.time = pralldata.operatingTimeStart + " - " + pralldata.operatingTimeEnd
			
			setTimeout(()=>{
				this.init()
			},1500)
		}
	}
</script>

<style>
	page {
		background-color: #F1F4FA;
	}
	
	.tabs {
		width: 95%;
		margin:  auto;
	}
	
	.tab {
		width: 100%;
		background-color: white;
		border-radius: 8px;
		/* margin: auto; */
		box-shadow: 1px 1px 10px #DCDFE6;
		padding-top: 10rpx;
		padding-bottom: 20rpx;
	}
	.fui-text__explain{
		font-size: 14px;
		color:#606266;
	}
	.tab_ops{
		padding: 40rpx;
		padding: 40rpx;
	}
</style>